package name.huzhenbo.java.patterns.observer;

import java.util.HashSet;
import java.util.Set;

class Clock implements Observable {
    private Set<Observer> observers = new HashSet<Observer>();
    private int hour;
    private int min;
    private int sec;

    public Clock(int hour, int min, int sec) {
        this.hour = hour;
        this.min = min;
        this.sec = sec;
    }

    public void addObserver(Observer observer) {
        if (observer == null) {
            throw new NullPointerException("The observer should not be null");
        }
        this.observers.add(observer);
    }

    public void increaseSecond() {
        sec++;
        notifyObservers();
    }

    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(this);
        }
    }

    public int getHour() {
        return hour;
    }

    public int getMin() {
        return min;
    }

    public int getSec() {
        return sec;
    }


    public int countObservers() {
        return observers.size();
    }

    public void deleteObserver(Observer observer) {
        observers.remove(observer);
    }
}
